package com.donde.coche;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

import android.util.Log;
import android.widget.Toast;

public class Compartir {
	private Double latitud;
	private Double longitud;
	private static Socket skCliente = null;
	
	public Compartir (Double lat, Double lon){
		latitud = lat;
		longitud = lon;
	}

	public Double getLatitud() {
		return latitud;
	}

	public void setLatitud(Double latitud) {
		this.latitud = latitud;
	}

	public Double getLongitud() {
		return longitud;
	}

	public void setLongitud(Double longitud) {
		this.longitud = longitud;
	}
	
	@Override
	public String toString(){
		return latitud+"#"+longitud; 
		
	}
	
	public static Compartir toCompartir(String s){
		String[] punto = s.split("#");
		return new Compartir(Double.parseDouble(punto[0]), Double.parseDouble(punto[1]));
	}
	
	public static Compartir ObtenerUltimaPos(){
    	String buf="0#1.93#1.342";
		sendData(buf);
		return toCompartir(readData());	
	}
	
	public boolean escribirContenido(){
		String buf = toString();
		buf = "1#"+buf;
		//log.w("escribircontenido","llego");
		return sendData(buf);
	}
	
	private static boolean initSocket(){
    	String IP= "10.42.0.1";
    	String IP2="10.10.50.227";
    	String IP3="192.168.1.10";
    	String IP4="10.10.50.160";
    	int Puerto = 9999;
    	//Socket skCliente;
    	try{
    		//Toast.makeText(this, "1", Toast.LENGTH_SHORT).show();
    		skCliente = new Socket(IP2,9999);
    		//Toast.makeText(this, "2", Toast.LENGTH_SHORT).show();
    		if (skCliente.isConnected() == true) {
    			//Toast.makeText(this, "Conectado", Toast.LENGTH_SHORT).show();
    			return true;
    		}
    		else {
    			//Toast.makeText(this, "Conexi�n rechazada. El servicio puede no estar disponible", Toast.LENGTH_SHORT).show();
    			return false;
    		}
    	} catch (Exception e) {
    		Log.e("Error conexion: ",""+e);
    		
    		//Toast.makeText(this, "Error en conexi�n"+e, Toast.LENGTH_SHORT).show();
    	}
    	return false;    	
    }
    private static boolean sendData(String data){
    	if (initSocket()==false) return false;
    	if (skCliente==null){
    		//Toast.makeText(this, "No est�s conectado", Toast.LENGTH_SHORT).show();
    		return false;
    	}
    	
    	//dataObject data =new dataObject(001,"yo1",new Date(), -1,1);
    	PrintWriter sSalida;
    	
    	//http://maps.google.es/?ll=43.051626,-8.105574&spn=2.191621,3.213501&t=m&z=8&layer=c&cbll=43.051626,-8.105574&panoid=8iE9wHbltHzFJi_I0JugHA&cbp=12,54.18,,0,0
    	//String buffer ="0#43.051626#-8.105574";//data.toString();//et1.getText().toString();// data.toString();//
    	
    	try {    		
    		sSalida = new PrintWriter(skCliente.getOutputStream(), true);
			sSalida.println(data);
			//sSalida.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			//Toast.makeText(this, "ERROR OutputStream "+ e.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
			return false;
		}
    	return true;
    	
    }
    
    private static String readData(){
    	
        BufferedReader sEntrada=null;
        String data=null;
		// Toast.makeText(this, "Esperando datos", Toast.LENGTH_SHORT).show();
		try {
			sEntrada = new BufferedReader(new InputStreamReader(skCliente.getInputStream()));
			data=sEntrada.readLine();
	        //et2.setText(data.toCharArray(), 0, data.length());
	        sEntrada.close();
	        skCliente.close();
	        return data;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
    }
}
